package com.newshub.app.ui.settings

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.newshub.app.R

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        
        // 设置ActionBar
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        
        // 显示设置Fragment
        val fragment: Fragment = SettingsFragment()
        supportFragmentManager.beginTransaction()
            .replace(android.R.id.content, fragment)
            .commit()
    }
    
    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
    
    class SettingsFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // 创建一个简单的设置列表
            val linearLayout = android.widget.LinearLayout(requireContext())
            linearLayout.orientation = android.widget.LinearLayout.VERTICAL
            linearLayout.setPadding(32, 32, 32, 32)
            
            // 标题
            val titleText = TextView(requireContext())
            titleText.text = "应用设置"
            titleText.textSize = 20f
            titleText.gravity = Gravity.CENTER
            titleText.setPadding(0, 0, 0, 32)
            linearLayout.addView(titleText)
            
            // 设置项1：清除缓存
            val clearCacheItem = createSettingItem("清除缓存", "清理应用缓存数据")
            clearCacheItem.setOnClickListener {
                android.widget.Toast.makeText(context, "缓存已清除", android.widget.Toast.LENGTH_SHORT).show()
            }
            linearLayout.addView(clearCacheItem)
            
            // 设置项2：检查更新
            val checkUpdateItem = createSettingItem("检查更新", "检查应用是否有新版本")
            checkUpdateItem.setOnClickListener {
                android.widget.Toast.makeText(context, "已是最新版本", android.widget.Toast.LENGTH_SHORT).show()
            }
            linearLayout.addView(checkUpdateItem)
            
            // 设置项3：关于应用
            val aboutItem = createSettingItem("关于应用", "查看应用版本和开发者信息")
            aboutItem.setOnClickListener {
                showAboutDialog()
            }
            linearLayout.addView(aboutItem)
            
            return linearLayout
        }
        
        private fun createSettingItem(title: String, subtitle: String): android.widget.LinearLayout {
            val itemLayout = android.widget.LinearLayout(requireContext())
            itemLayout.orientation = android.widget.LinearLayout.VERTICAL
            itemLayout.setPadding(16, 16, 16, 16)
            itemLayout.background = android.graphics.drawable.ColorDrawable(android.graphics.Color.parseColor("#F5F5F5"))
            
            val titleText = TextView(requireContext())
            titleText.text = title
            titleText.textSize = 16f
            titleText.setTextColor(android.graphics.Color.BLACK)
            
            val subtitleText = TextView(requireContext())
            subtitleText.text = subtitle
            subtitleText.textSize = 14f
            subtitleText.setTextColor(android.graphics.Color.GRAY)
            subtitleText.setPadding(0, 4, 0, 0)
            
            itemLayout.addView(titleText)
            itemLayout.addView(subtitleText)
            
            return itemLayout
        }
        
        private fun showAboutDialog() {
            val builder = androidx.appcompat.app.AlertDialog.Builder(requireContext())
            builder.setTitle("关于新闻中心")
            builder.setMessage("新闻中心 v1.0\n\n这是一个简洁的新闻阅读应用，为您提供最新的新闻资讯。\n\n开发团队：新闻中心团队")
            builder.setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
            }
            builder.show()
        }
    }
}